﻿using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
using System;
using System.Collections.Generic;
using System.Text;

namespace SCM.Manage
{
    /// <summary>
    /// 单选框
    /// </summary>
    [HtmlTargetElement(RadioTagName)]
    public class RadioTagHelper : TagHelper
    {
        private const string RadioTagName = "cl-radio";
        private const string ForAttributeName = "asp-for";
        private const string ItemsAttributeName = "asp-items";
        private const string ClassAttributeName = "asp-class";
        [ViewContext]
        public ViewContext ViewContext { get; set; }

        [HtmlAttributeName(ForAttributeName)]
        public ModelExpression For { get; set; }

        [HtmlAttributeName(ItemsAttributeName)]
        public IEnumerable<SelectListItem> Items { get; set; }

        [HtmlAttributeName(ClassAttributeName)]
        public string Class { get; set; }
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (For == null)
            {
                throw new ArgumentException("必须绑定模型");
            }
            output.TagName = "div";

            output.Content.Clear();
            foreach (var item in Items)
            {
                //var radio = new TagBuilder("input");
                //radio.TagRenderMode = TagRenderMode.SelfClosing;
                //radio.Attributes.Add("id", ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name));
                //radio.Attributes.Add("name", ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name));
                //radio.Attributes.Add("value", item.Value);
                //radio.Attributes.Add("title", item.Text);
                //radio.Attributes.Add("type", "radio");
                //if(!string.IsNullOrWhiteSpace(Class))
                //{
                //    radio.AddCssClass(Class);
                //}

                //if (item.Disabled )
                //{
                //    radio.Attributes.Add("disabled", "disabled");
                //}
                //if (item.Selected || item.Value == For.Model?.ToString())
                //{
                //    radio.Attributes.Add("checked", "checked");
                //}
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append($"<input ");

                stringBuilder.Append($"id=\"{ ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name)}\" ");
                stringBuilder.Append($"name=\"{ ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(For.Name)}\" ");
                stringBuilder.Append($"value=\"{  item.Value}\" ");
                stringBuilder.Append($"class=\"radio\" ");
                stringBuilder.Append($"title=\"{  item.Text}\" ");
                if (item.Disabled)
                {
                    stringBuilder.Append($"disabled=\"disabled\" ");
                }
                if (item.Selected || item.Value == For.Model?.ToString())
                {
                    stringBuilder.Append($"checked=\"checked\" ");
                }
                stringBuilder.Append($"type=\"radio\" > ");
                stringBuilder.Append(item.Text);
                stringBuilder.Append($"</input> ");

                //if (item.Selected || item.Value == For.Model?.ToString())
                //{
                //  radio.Attributes.Add("checked", "checked");
                //}
                //output.Content.AppendHtml(radio);
                output.Content.AppendHtml(stringBuilder.ToString());
            }
        }
    }

    /// <summary>
    /// 枚举选择器扩展
    /// </summary>
    public static class RadioTagItemHelper
    {
        public static IEnumerable<SelectListItem> GetEnumItemList(this IHtmlHelper helper, Type enumType)
        {
            List<SelectListItem> select = new List<SelectListItem>();
            select.Add(new SelectListItem() { Text = "请选择", Value = "", Selected = true });

            var list = helper.GetEnumSelectList(enumType);
            select.AddRange(list);
            return select;
        }
    }
}
